package j2024.j202408;

public class j0815 {
    /**
     * 714. 买卖股票的最佳时机含手续费
     * @param prices
     * @param fee
     * @return
     */
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[0] = -prices[0];
        for (int i = 1; i < n; i++) {
            f[i] = Math.max(f[i-1],g[i-1]-prices[i]);
            g[i] = Math.max(g[i-1],f[i-1]+prices[i]-fee);
        }
        return Math.max(f[n-1],g[n-1]);
    }

    /**
     * 123. 买卖股票的最佳时机 III
     * @param prices
     * @return
     */
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] f  = new int[n][3];
        int[][] g  = new int[n][3];
        for (int i = 0; i < 3; i++) {
            f[0][i] = g[0][i] = -0x3f3f3f3f;
        }
        f[0][0] = -prices[0];
        g[0][0] = 0;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < 3; j++) {
                f[i][j] = Math.max(f[i-1][j],g[i-1][j]-prices[i]);
                g[i][j] = g[i-1][j];
                if(j-1>=0){
                    g[i][j] = Math.max(g[i][j],f[i-1][j-1]+prices[i]);
                }
            }
        }
        return Math.max(g[n-1][0],Math.max(g[n-1][1],g[n-1][2]));
    }

    /**
     * 188. 买卖股票的最佳时机 IV
     * @param k
     * @param prices
     * @return
     */
    public int maxProfit(int k, int[] prices) {
        int n = prices.length;
        int[][] f = new int[n][k+1];
        int[][] g = new int[n][k+1];
        for (int i = 0; i <= k; i++) {
            f[0][i] = g[0][i] = -0x3f3f3f3f;
        }
        f[0][0] = -prices[0];
        g[0][0]  = 0;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= k; j++) {
                f[i][j] = Math.max(f[i-1][j],g[i-1][j]-prices[i]);
                g[i][j] = g[i-1][j];
                if(j-1>=0){
                    g[i][j] = Math.max(g[i][j],f[i-1][j-1]+prices[i]);
                }
            }
        }
        int ret = 0;
        for (int i = 0; i <= k; i++) {
            ret = Math.max(ret,g[n-1][i]);
        }
        return ret;
    }

    /**
     * 53. 最大子数组和
     * @param nums
     * @return
     */
    public int maxSubArray(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n+1];
        int ret = Integer.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            dp[i] = Math.max(nums[i-1],dp[i-1]+nums[i-1]);
            ret = Math.max(ret,dp[i]);
        }
        return ret;
    }

    /**
     * 918. 环形子数组的最大和
     * @param nums
     * @return
     */
    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        int[] f = new  int[n+1];
        int[] g = new  int[n+1];
        int fmin = Integer.MAX_VALUE,fmax = Integer.MIN_VALUE;
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            int x = nums[i-1];
            f[i] = Math.max(x,x+f[i-1]);
            fmax = Math.max(fmax,f[i]);
            g[i] = Math.min(x,g[i-1]+x);
            fmin = Math.min(fmin,g[i]);
            sum+=x;
        }
        return sum == fmin?fmax : Math.max(fmax,sum-fmin);
    }

    /**
     * 152. 乘积最大子数组
     * @param nums
     * @return
     */
    public static int maxProduct(int[] nums) {
        int n = nums.length;
        double[] f = new double[n+1];
        double[] g = new double[n+1];
        f[0] = g[0] =1;
        double ret  = Long.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            double x = nums[i-1],y = f[i-1]*nums[i-1],z = g[i-1]*nums[i-1];
            f[i] = Math.max(x,Math.max(y,z));
            g[i] = Math.min(x,Math.min(y,z));
            ret  = Math.max(f[i],ret);
        }
        return (int)ret;
    }

    public static void main(String[] args) {
        int[] arr = {-2};
        int a = maxProduct(arr);
    }

    /**
     * 1567. 乘积为正数的最长子数组长度
     * @param nums
     * @return
     */
    public int getMaxLen(int[] nums) {
        int n = nums.length;
        int[] f =new int[n+1];
        int[] g = new int[n+1];
        int ret = Integer.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            if(nums[i-1]>0){
                f[i] = f[i-1]+1;
                g[i]  = g[i-1] == 0?0 : g[i-1]+1;
            } else if(nums[i-1]<0){
                f[i] = g[i-1]==0?0:g[i-1]+1;
                g[i] = f[i-1]+1;
            }
            ret = Math.max(ret,f[i]);
        }
        return  ret;
    }


}
